﻿using com.nm.library.Excels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.library;
using System.Data;
using com.nm;
using com.nm.db.Models.Terminal;
using System.ComponentModel.Composition;
using george.Helper;

namespace TerminalAPI.ImportExcel
{
    [Export(typeof(ExcelImport))]
    public class DisplayShelfExcel : ExcelImport
    {
        /// <summary>
        /// Excel字段映射及校验缓存
        /// </summary>
        /// <returns>字段映射</returns>
        public override Dictionary<string, ImportVerify> DictFields => dictFields;

        private static Dictionary<string, ImportVerify> dictFields = new List<ImportVerify> {
            new ImportVerify{ ColumnName="年份",FieldName="Year",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,6,true,true)},
            new ImportVerify{ ColumnName="季节",FieldName="Season",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,6,true,true)},
            new ImportVerify{ ColumnName="货架编号",FieldName="GSCode",DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true) },
            new ImportVerify{ ColumnName="货架名称",FieldName="GSName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,50,true,true)},
            new ImportVerify{ ColumnName="A级陈列标准SKC",FieldName="ASKC" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,true,false)},
            new ImportVerify{ ColumnName="B级陈列标准SKC",FieldName="BSKC",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,true,false)},
            new ImportVerify{ ColumnName="C级陈列标准SKC",FieldName="CSKC",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,true,false)}
        }.ToDictionary(e => e.ColumnName, e => e);

        public override ExcelImportType Type => ExcelImportType.SHELFDISPLAY;

        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int userId)
        {
            var index = dt.Rows.Count;
            var msg = $"成功导入{index}条数据";
            try
            {
                var dc = new TerminalEntities();
                foreach (DataRow item in dt.Rows)
                {
                    #region 校验

                    var gsCode = item["GSCode"].ToString().StartTrim();
                    var good = dc.Tm_GoodShelf.FirstOrDefault(t => t.GSCode == gsCode);
                    if(good==null)
                    {
                        SysChangeLog.WriteLog(userId,2, gsCode, "货架陈列信息", $"货架编号:{gsCode}不存在");
                        continue;
                    }
                    #endregion

                    #region 写入
                    var query = dc.Tm_ShelfDisplay.FirstOrDefault(t => t.GSID == good.GSID);
                    var ASKC = Convert.ToInt16(item["ASKC"].ToString());
                    var BSKC = Convert.ToInt16(item["BSKC"].ToString());
                    var CSKC = Convert.ToInt16(item["CSKC"].ToString());
                    if (query != null)
                    {
                        query.ASKC = ASKC;
                        query.BSKC = BSKC;
                        query.CSKC = CSKC;
                        query.Season = item["Season"].ToString();
                        query.ModifyDate = DateTime.Now;
                        query.Year =item["Year"].ToString();
                    }
                    else
                    {
                        var shelf = new Tm_ShelfDisplay
                        {
                            Year = item["Year"].ToString(),
                            Season = item["Season"].ToString(),
                            GSID = good.GSID,
                            ASKC = ASKC,
                            BSKC = BSKC,
                            CSKC = CSKC,
                            CreateDate = DateTime.Now,
                            IsRemove = false,
                            ModifyDate = DateTime.Now,
                            Operator = userId
                        };
                        dc.Tm_ShelfDisplay.Add(shelf);
                    }
                }
                dc.SaveChanges();
                #endregion
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return new ImportResult()
                {
                    IsSuccess = false,
                    ExtraInfo = ex,
                    Message = ex.Message
                };
            }
            return new ImportResult()
            {
                IsSuccess = true,
                ExtraInfo = "导入成功",
                Message = msg
            };
        }

        public override DataTable GetExportData(string Keys)
        {
            throw new NotImplementedException();
        }
    }
}
